﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SC_BLL
{
    /// <summary>
    ///Client 的摘要说明
    /// </summary>
    public static class Client
    {
        /// <summary>
        /// 获取用户昵称
        /// </summary>
        public static string UserNick
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Request.Cookies[Config.CookiesName] != null)
                    {
                        return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[Config.CookiesName]["visitor_nick"].ToString());
                    }
                    else
                    {
                        return "";
                    }
                }
                catch (Exception exp)
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 获取用户授权
        /// </summary>
        public static string Session
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Request.Cookies[Config.CookiesName] != null)
                    {
                        return HttpContext.Current.Request.Cookies[Config.CookiesName]["top_session"].ToString();
                    }
                    else
                    {
                        return "";
                    }
                }
                catch (Exception exp)
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 保存用户指定数据  -- 仅debug使用
        /// </summary>
        /// <param name="nick"></param>
        /// <param name="sessionkey"></param>
        public static void SetCookies(string nick, string sessionkey)
        {
            HttpContext.Current.Response.Cookies[Config.CookiesName]["visitor_nick"] = HttpUtility.UrlEncode(nick);
            HttpContext.Current.Response.Cookies[Config.CookiesName]["top_session"] = sessionkey;
            HttpContext.Current.Response.Cookies[Config.CookiesName].Expires = DateTime.Now.AddMonths(1);
        }

        /// <summary>
        /// 用户是否登陆
        /// </summary>
        public static bool IsLogin
        {
            get
            {
                if (HttpContext.Current.Request.Cookies[Config.CookiesName] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[Config.CookiesName].Value))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 退出用户清除Cookie
        /// </summary>
        /// <returns></returns>
        public static int Logout()
        {
            //HttpContext.Current.Response.Cookies.Remove(Config.CookiesName);
            HttpCookie cookie = HttpContext.Current.Request.Cookies[Config.CookiesName];
            cookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.AppendCookie(cookie);
            return 1;
        }
    }
}
